package com.example.gfsquadwebviewproject.net;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import com.example.gfsquadwebviewproject.data.Constant;

/**
 * Created by huangbing on 2017/7/12.
 */

public class NetWorkStateReceiver extends BroadcastReceiver {
    Constant constant;
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isInternet ;
        int netWordType ;

        try {
            if (context!=null) {
                //检测API是不是小于21，因为到了API21之后getNetworkInfo(int networkType)方法被弃用
                if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
                    //获得ConnectivityManager对象
                    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    //获取ConnectivityManager对象对应的NetworkInfo对象
                    //获取WIFI连接的信息
                    NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                    //获取移动数据连接的信息
                    NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                    if (!wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                        isInternet = true;
                        netWordType = 1;
                    } else if (wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
                        isInternet = true;
                        netWordType = 2;
                    } else if (wifiNetworkInfo.isConnected() && dataNetworkInfo.isConnected()) {
                        isInternet = true;
                        netWordType = 2;
                    } else {
                        isInternet = false;
                        netWordType = 0;
                    }
                } else {
                    //获得ConnectivityManager对象
                    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

                    //获取所有网络连接的信息
                    Network[] networks = connMgr.getAllNetworks();

                    //通过循环将网络信息逐个取出来
                    isInternet = false;
                    netWordType = 0;

                    for (int i = 0; i < networks.length; i++) {
                        //获取ConnectivityManager对象对应的NetworkInfo对象
                        NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);

                        if (networkInfo.isConnected()) {
                            isInternet = true;
                            //以太网
                            if (networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                                if (netWordType < 3) {
                                    netWordType = 3;
                                }
                            }
                            //检测到有wifi连接，连接状态必为true
                            else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                                if (netWordType < 2) {
                                    netWordType = 2;
                                }
                            }
                            //检测到有数据连接，但是并连接状态未生效，此种状态为wifi和数据同时已连接，以wifi连接优先
                            else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                                if (netWordType < 1) {
                                    netWordType = 1;
                                }
                            }
//                        break;
                        }
                    }
                }

                constant.isInternet = isInternet;
                constant.netWordType = netWordType;
                if (context != null) {
                    context.sendBroadcast(new Intent(constant.ACTION_NETWORK));
                }
            }
        }
        catch (Exception e){

        }

    }
}